package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;

/**
 * @Author 王加鑫
 * @Date 2021/5/4 20:41
 * @Version 1.0
 */
public class StudentService {
    private StudentDao studentDao = new StudentDao();

    /*
    Service层添加学生
     */
    public boolean addStudent(Student student) {
        return studentDao.addStudent(student);
    }

    /*
    Service判断Id是否重复
    index为-1表示没有被占用
    index为数组索引值说明数组中已经被占用了

    这个方法的作用：查询id是否被占用并返回数组中没有的id的索引值
     */
    public boolean isExists(String sId) {
        //标记位思想:默认认为数组没有重复sId,flag为false
        boolean flag = false;
        //指挥dao层查出来所有的数据再进行判断
        Student[] students = studentDao.findAllStudent();
        for (int i = 0; i < students.length; i++) {
            //当数组元素不为空且取出来的值等于传过来的sId值时,说明id重复
            if(students[i] != null && students[i].getId().equals(sId)){
                flag = true;
            }
        }
        return flag;
    }


    /*
    Service层查询所有学生
    进行逻辑处理
     */
    public Student[] findAllStudent() {
        boolean flag = false;
        //指挥dao层查询出所有的学生信息
        Student[] students = studentDao.findAllStudent();
        for (int i = 0; i < students.length; i++) {
            if(students[i] != null){
                flag = true;
            }
        }
        if(flag){
            return students;
        }
        return null;
    }

    /*
    删除学生
     */
    public void deleteStudent(String sId) {
       studentDao.deleteStudent(sId);
    }

    /*
    根据学号查询Student对象
     */
   /* public Student selectStudentById(String sId) {
        int index = isExists(sId);
        Student[] students = studentDao.findAllStudent();
        return studentDao.selectStudentByIndex(index);

    }
    */

    public void updateStudent(String sId, Student student) {
        studentDao.updateStudent(sId,student);
    }
}
